home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / ipc / mainshmncli.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  2KB  |  93 lines

  1. #include    <stdio.h>
  2. #include    <sys/types.h>
  3. #include    <sys/ipc.h>
  4. #include    <sys/shm.h>
  5.  
  6. #include    "shm.h"
  7.  
  8. int    clisem, servsem;    /* semaphore IDs */
  9.  
  10. int    shmid[NBUFF];        /* shared memory IDs */
  11. Mesg    *mesgptr[NBUFF];    /* ptr to message structures, which are
  12.                    in the shared memory segment */
  13.  
  14. main()
  15. {
  16.     register int    i;
  17.  
  18.     /*
  19.      * Get the shared memory segments and attach them.
  20.      * We don't specify IPC_CREAT, assuming the server creates them.
  21.      */
  22.  
  23.     for (i = 0; i < NBUFF; i++) {
  24.         if ( (shmid[i] = shmget(SHMKEY + i, sizeof(Mesg), 0)) < 0)
  25.             err_sys("client: can't get shared memory %d", i);
  26.         if ( (mesgptr[i] = (Mesg *) shmat(shmid[i], (char *) 0, 0))
  27.                                 == (Mesg *) -1)
  28.             err_sys("client: can't attach shared memory %d", i);
  29.     }
  30.  
  31.     /*
  32.      * Open the two semaphores.
  33.      */
  34.  
  35.     if ( (clisem = sem_open(SEMKEY1)) < 0)
  36.         err_sys("client: can't open client semaphore");
  37.     if ( (servsem = sem_open(SEMKEY2)) < 0)
  38.         err_sys("client: can't open server semaphore");
  39.  
  40.     client();
  41.  
  42.     /*
  43.      * Detach and remove the shared memory segments and
  44.      * close the semaphores.
  45.      */
  46.  
  47.     for (i = 0; i < NBUFF; i++) {
  48.         if (shmdt(mesgptr[i]) < 0)
  49.             err_sys("client: can't detach shared memory %d", i);
  50.         if (shmctl(shmid[i], IPC_RMID, (struct shmid_ds *) 0) < 0)
  51.             err_sys("client: can't remove shared memory %d", i);
  52.     }
  53.  
  54.     sem_close(clisem);
  55.     sem_close(servsem);
  56.  
  57.     exit(0);
  58. }
  59.  
  60. client()
  61. {
  62.     int    i, n;
  63.  
  64.     /*
  65.      * Read the filename from standard input, write it to shared memory.
  66.      */
  67.  
  68.     sem_wait(clisem);        /* wait for server to initialize */
  69.     if (fgets(mesgptr[0]->mesg_data, MAXMESGDATA, stdin) == NULL)
  70.         err_sys("filename read error");
  71.  
  72.     n = strlen(mesgptr[0]->mesg_data);
  73.     if (mesgptr[0]->mesg_data[n-1] == '\n')
  74.         n--;            /* ignore newline from fgets() */
  75.     mesgptr[0]->mesg_len = n;
  76.     sem_signal(servsem);        /* wake up server */
  77.  
  78.     for ( ; ; ) {
  79.         for (i = 0; i < NBUFF; i++) {
  80.             sem_wait(clisem);
  81.             if ( (n = mesgptr[i]->mesg_len) <= 0)
  82.                 goto alldone;
  83.             if (write(1, mesgptr[i]->mesg_data, n) != n)
  84.                 err_sys("data write error");
  85.             sem_signal(servsem);
  86.         }
  87.     }
  88.  
  89. alldone:
  90.     if (n < 0)
  91.         err_sys("data read error");
  92. }
  93.